package com.ruoyi.web.controller.scrap;

import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.DeleteStatus;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.scrap.domain.ScrapApplyDetail;
import com.ruoyi.scrap.domain.ScrapStockIn;
import com.ruoyi.scrap.service.ScrapApplyDetailService;
import com.ruoyi.scrap.service.ScrapStockInService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

@RestController
@RequestMapping("/scrap/stock/in")
public class ScrapStockInController extends BaseController {

    @Resource
    RedisCache redisCache;

    @Resource
    ScrapStockInService stockInService;

    @Resource
    ScrapApplyDetailService detailService;

    @GetMapping("/list")
    public TableDataInfo list(ScrapStockIn stockIn)
    {
        startPage();
        List<ScrapStockIn> list = stockInService.getScrapStockIn(stockIn);
        return getDataTable(list);
    }

    @Log(title = "报废入库记录单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScrapStockIn stockIn)
    {
        List<ScrapStockIn> list = stockInService.getScrapStockIn(stockIn);
        ExcelUtil<ScrapStockIn> util = new ExcelUtil<ScrapStockIn>(ScrapStockIn.class);
        util.exportExcel(response, list, "sheet1","废旧物资回收入库记录单");
    }

    @GetMapping(value = "/info/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(stockInService.getById(id));
    }

    @Log(title = "报废入库记录单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ScrapStockIn stockIn) {

        SysUser loginUser = getLoginUser().getUser();

        stockIn.setDelFlag(DeleteStatus.OK.getCode());
        stockIn.setCreateTime(new Date());
        stockIn.setCreateBy(loginUser.getUserName());
        stockIn.setUpdateTime(new Date());
        stockIn.setUpdateBy(loginUser.getUserName());
        stockIn.setCreateUser(loginUser.getUserId());
        stockIn.setCreateDept(loginUser.getDeptId());
        stockIn.setStatus("0");

        stockIn.setId(redisCache.getOrderNo(Constants.SCRAP_STOCK_IN_KEY));

        return toAjax(stockInService.save(stockIn));
    }

    @Log(title = "报废入库记录单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ScrapStockIn stockIn) {

        long exist = detailService.count(Wrappers.lambdaQuery(ScrapApplyDetail.class)
                .eq(ScrapApplyDetail::getStockId,stockIn.getId())
                .eq(ScrapApplyDetail::getDelFlag,DeleteStatus.OK.getCode()));

        if(exist > 0){
            return AjaxResult.error("当前记录单已提交处置申请，无法修改");
        }

        SysUser loginUser = getLoginUser().getUser();

        stockIn.setUpdateTime(new Date());
        stockIn.setUpdateBy(loginUser.getUserName());

        return toAjax(stockInService.updateById(stockIn));
    }

    @Log(title = "报废入库记录单", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {

        SysUser loginUser = getLoginUser().getUser();

        List<ScrapStockIn> remove = Arrays.asList(ids).stream().map( x->{

            ScrapStockIn stockIn = new ScrapStockIn();

            stockIn.setId(x);
            stockIn.setDelFlag(DeleteStatus.DELETED.getCode());
            stockIn.setUpdateTime(new Date());
            stockIn.setUpdateBy(loginUser.getUserName());

            return stockIn;

        }).collect(Collectors.toList());

        if(ObjectUtils.isEmpty(remove)){
            return AjaxResult.error("请选择要删除的记录单");
        }

        return toAjax(stockInService.updateBatchById(remove));
    }

}
